<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { contentApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  import xmUpload from '@/components/Upload/xm-upload.vue';
  const formLabelWidth = '120px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:'',
    pageType:[] as Array<any>,
  })
  interface formQuert {
    id:number
    room_uuids:string
    is_show:number
    type:number
    weight:number
    resource_url:string
    resource_url2:string
    start_time:number
    end_time:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    room_uuids: [{ required: true, message: "请输入房间uid，如果有多个，用英文逗号分隔", trigger: "blur" }],
    type: [{ required: true, message: "请选择展示位置", trigger: "blur" }],
    is_show: [{ required: true, message: "请选择是否展示", trigger: "blur" }],
    weight: [{ required: true, message: "请输入展示权重，正整数, 从大到小排序", trigger: "blur" }],
    resource_url: [{ required: true, message: "请上传资源图", trigger: "blur" }],
    start_time: [{ required: true, message: "选择开始时间", trigger: "blur" }],
    end_time: [{ required: true, message: "选择结束时间", trigger: "blur" }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      let submitSet = false
      const { id, resource_url } = form
      form.resource_url2 = resource_url
      if(id) {
        await contentApi.roomBackUpdate(form).then(()=>{
          ToastSuccess('编辑成功')
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }else {
        await contentApi.roomBackAdd(form).then(()=>{
          ToastSuccess('新增成功')
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }
      if(submitSet) {
        loading.close()
        closeDialog()
        $emit('updateList')
      }
    })
  }
  const openDialog = async(query:any = {})=> {
    const {
      pageType=[], id=0, room_uuids='',
      type='',is_show='',weight='',resource_url='',
      start_time='', end_time=''
    } = query
    Object.assign(form,{
      id, room_uuids,
      type,is_show,weight,resource_url,
      start_time, end_time
    })
    action.title = id?'编辑背景图':'新增背景图'
    action.pageType = pageType
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="40%"
      :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        
        <el-form-item :label-width="formLabelWidth" label="展示位置" prop="type">
          <el-select v-model="form.type" placeholder="请选择展示位置">
            <el-option
              v-for="item in action.pageType"
              :label="item.value"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.type == 4" :label-width="formLabelWidth" label="指定房间" prop="room_uuids">
          <el-input
            v-model="form.room_uuids"
            :autosize="{ minRows: 3, maxRows: 3}"
            type="textarea"
            placeholder="请输入房间uid，如果有多个，用英文逗号分隔"
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="是否展示" prop="is_show">
          <el-select v-model="form.is_show" placeholder="请选择是否展示">
            <el-option label="显示" :value="1" />
            <el-option label="不显示" :value="0" />
          </el-select>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="展示权重" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入展示权重，正整数, 从大到小排序"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="资源图" prop="resource_url">
          <xmUpload
            listType="picture-card"
            :data="{type:21}"
            v-model:file-url="form.resource_url"
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="开始时间" prop="start_time">
          <el-date-picker
            v-model="form.start_time"
            type="datetime"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="X"
            placeholder="选择开始时间" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="结束时间" prop="end_time">
          <el-date-picker
            v-model="form.end_time"
            type="datetime"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="X"
            placeholder="选择结束时间" />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>